<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function array(num1, num2) {
      let result = 0;
      let sum = 0;
      let arr = Array(num1.length + 1).fill(0)
      arr[0] = 1
      for (let num of num1) {
        sum += num;
        if (sum >= num2) {
          result += arr[sum - num2]
        }
        arr[sum] += 1
      }
      return result;
    };
    console.log(array([1, 0, 1, 0, 1], 2));
  </script>
</body>

</html>